package com.ruoyi.json;
import com.ruoyi.HouseErrorMsg;
import com.ruoyi.HouseException;

import java.beans.PropertyDescriptor;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class JsonTools extends BeanInfoUtil{


    public static String lstMapToJson(List list)throws HouseException, JSONException{
        JSONArray jsonLst = new JSONArray();
        for(int i=0;i<list.size();i++){
            Object obj = list.get(i);
            JSONObject json = JsonTools.mapToJson(obj);
            jsonLst.put(json);
        }
        return jsonLst.toString();
    }

    public static String lstBeanToJson(List list)throws HouseException{
        JSONArray jsonLst = new JSONArray();
        for(int i=0;i<list.size();i++){
            Object obj = list.get(i);
            JSONObject json = JsonTools.beanToJson(obj);
            jsonLst.put(json);
        }
        return jsonLst.toString();
    }

    public static JSONObject mapToJson(Object obj)throws HouseException, JSONException{
        try{
            Map map = (Map)obj;
            Set set = map.keySet();
            Iterator iter = set.iterator();
            JSONObject json = new JSONObject();
            while(iter.hasNext()){
                String key = (String)iter.next();
                String val = map.get(key.toUpperCase())==null?"":map.get(key.toUpperCase()).toString();
                json.put(key.toLowerCase(), val);
            }
            return json;
        }catch(Exception e){
            throw new HouseException(HouseErrorMsg.ERR_DEFAULT,"对象转换JSON出错！",e);
        }
    }

    public static JSONObject beanToJson(Object obj)throws HouseException{
        try{
            PropertyDescriptor[] props = BeanInfoUtil.getPropertyDescriptors(obj);
            JSONObject json = new JSONObject();
            for (int i = 0; i < props.length; i++) {
                if(BeanInfoUtil.rightName(props[i])){
                    Object value=props[i].getReadMethod().invoke(obj,null);
                    String val = "";
                    if(value instanceof java.lang.Double){
                        val = new BigDecimal(BeanInfoUtil.getReadMethod(props[i]).invoke(obj,null)==null?"0":BeanInfoUtil.getReadMethod(props[i]).invoke(obj,null).toString()).toString();
                    }else{
                        val = BeanInfoUtil.getReadMethod(props[i]).invoke(obj,null)==null?"":BeanInfoUtil.getReadMethod(props[i]).invoke(obj,null).toString();
                    }

                    json.put(props[i].getName(),val.trim());
                }
            }
            return json;
        }catch(Exception e){
            throw new HouseException(HouseErrorMsg.ERR_DEFAULT,"对象转换JSON出错！",e);
        }
    }
    public static JSONArray stringToJsonArray(String obj)throws HouseException{
        try{
            PropertyDescriptor[] props = BeanInfoUtil.getPropertyDescriptors(obj);
            JSONArray json = new JSONArray();
//          List<Map<String,String>> list = new ArrayList<Map<String,String>>();
            String ee = obj.replace("{", "");
            String aa = ee.replace("}", "");
            JSONObject json01 = new JSONObject();
            String[] mm = aa.split(",");
            for(int j=0;j<mm.length;j++) {
                String lengStr[] = mm[j].split(":");
                String qq = mm[j].split(":")[0].replace("\"", "");
                String gg = mm[j].split(":")[1].replace("\"", "");
                json01.accumulate(qq, gg);
            }
            json.put(json01);

            return json;
        }catch(Exception e){
            throw new HouseException(HouseErrorMsg.ERR_DEFAULT,"对象转换JSON出错！",e);
        }
    }
}
